package com.yiwise.config;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;

import java.util.concurrent.Callable;

public class NewMDCDecoratorCallable implements Callable {
    private static final String mdcKey = "MDC_LOG_ID";

    private Callable callable;
    private String mdcValue;

    public NewMDCDecoratorCallable(Callable callable) {
        this.callable = callable;
        this.mdcValue = RandomStringUtils.randomAlphanumeric(8);
    }

    public NewMDCDecoratorCallable(String mdcValue, Callable callable) {
        this.callable = callable;
        this.mdcValue = mdcValue;
        if (StringUtils.isEmpty(this.mdcValue)) {
            this.mdcValue = RandomStringUtils.randomAlphanumeric(8);
        }
    }

    @Override
    public Object call() throws Exception {
        MDC.put(mdcKey, mdcValue);
        return callable.call();
    }
}